package com.example.demo03.util;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class BaseController {
    // 静态方法解析前端提交的json格式的数据
    public static <T> T parseJsonToPojo(HttpServletRequest request, Class<T> clazz) throws Exception {
        T obj = JSON.parseObject(request.getInputStream(), clazz);
        // 返回对象
        return obj;
    }

    // 静态方法将后端的json数据响应给前端
    public static void ResponseJsonToBrowser(HttpServletResponse response, Object obj) throws IOException {
        // 将obj转换成json字符串
        String s = JSON.toJSONString(obj);
        // 响应给浏览器
        response.getWriter().print(s);
    }
}
